package com.tianzeculture.stagebao.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.home.MainActivity;
import com.tianzeculture.stagebao.activity.login.RegisterActivity;
import com.tianzeculture.stagebao.base.BaseActivity;
import com.tianzeculture.stagebao.bean.LoginOtherBean;
import com.tianzeculture.stagebao.bean.ResultBean;
import com.tianzeculture.stagebao.bean.User;
import com.tianzeculture.stagebao.dialog.ChoiceJobDialog;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.Util;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/20.
 */

public class BindPhoneActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.edtPhone)
    EditText edtPhone;
    @BindView(R.id.tvGetCode)
    TextView tvGetCode;
    @BindView(R.id.edtCode)
    EditText edtCode;
    @BindView(R.id.btnRegister)
    Button btnRegister;

    private int GET_CODE = 101;//获取验证码
    private String phone = "";//获取验证码的手机号
    private String code = "";//验证码
    private int loginByOtherId = 10;

    @Request
    UserRestUsage userRestUsage = null;

    ChoiceJobDialog choiceJobDialog = null;
    BindPhoneActivity.TimeCount time;

    LoginOtherBean loginOtherBean;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_bind_phone;
    }


    @Override
    public void initUI() {
        super.initUI();
        loginOtherBean = (LoginOtherBean) getIntent().getSerializableExtra("data");
        tvTitle.setText("绑定手机号");
        time = new BindPhoneActivity.TimeCount(10000, 1000);
    }


    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == GET_CODE) {
            ToastUtil.showShort(this,msg.getMessage());
            if (msg.getObj() != null) {
                ResultBean resultBean = (ResultBean) msg.getObj();
                code = resultBean.getCode();
            }
        }
        if (taskId == loginByOtherId) {
            dismissProgressDialog();
            if (msg.getData()){
                User user = (User) msg.getObj();
                Util.JOB = user.getUserType();
                Util.UserId = user.getUserId();
                Util.Token = user.getToken();
                startActivity(new Intent(this, MainActivity.class));
                finish();
            }
        }
    }

    /**
     * 非空判断
     *
     * @return
     */
    private boolean checkEntity() {
        if ("".equals(edtPhone.getText().toString().trim())) {
            ToastUtil.showShort(BindPhoneActivity.this, "手机号码不能为空!");
            return false;
        } else if ("".equals(edtCode.getText().toString().trim())) {
            ToastUtil.showShort(BindPhoneActivity.this, "验证码不能为空!");
            return false;
//        } else if ("".equals(edtPwd.getText().toString().trim())) {
//            ToastUtil.showShort(BindPhoneActivity.this, "密码不能为空!");
//            return false;
//        } else if ("".equals(edtPwdAgain.getText().toString().trim())) {
//            ToastUtil.showShort(BindPhoneActivity.this, "再次密码不能为空!");
//            return false;
//        } else if (!edtPwd.getText().toString().trim().equals(edtPwdAgain.getText().toString().trim())) {
//            ToastUtil.showShort(BindPhoneActivity.this, "两次密码不一致!");
//            return false;
//        } else if (!cboxPact.isChecked()) {
//            ToastUtil.showShort(this, "请阅读并同意舞台宝协议！");
        }
        return true;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tvGetCode, R.id.btnRegister})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvGetCode:
                if (Util.isPhone(edtPhone.getText().toString().trim())) {//1注册 2重置密码
                    phone = edtPhone.getText().toString().trim();//保存获取验证码的手机号
                    userRestUsage.getCode(GET_CODE, edtPhone.getText().toString().trim(), 1);
                    time.start();
                }
                break;
            case R.id.btnRegister:
                if (checkEntity()) {
                    if (!edtCode.getText().toString().trim().equals(code)) {
                        ToastUtil.showShort(this, "验证码错误!");
                        return;
                    } else if (!phone.equals(edtPhone.getText().toString().trim())) {
                        ToastUtil.showShort(this, "该手机号码与获取验证码手机不匹配!");
                        return;
                    }
                    choiceJobDialog = new ChoiceJobDialog(this);
                    choiceJobDialog.setChoiceState(0);//未选择
                    choiceJobDialog.setOnSelectClickListener(new ChoiceJobDialog.OnSelectClickListener() {
                        @Override
                        public void onOk(int state) {
                            if (state == 1) {
                                ToastUtil.showShort(BindPhoneActivity.this, "艺人");
                                showProgressDialog("正在绑定...");
                                Map<String, String> param = new HashMap<String, String>();
                                param.put("userType", "0");
                                param.put("loginType", loginOtherBean.getLoginType());
                                param.put("account", loginOtherBean.getAccount());
                                param.put("username", loginOtherBean.getUsername());
                                param.put("userSex", loginOtherBean.getUserSex());
                                param.put("userArea", loginOtherBean.getUserArea());
                                param.put("userHeader", loginOtherBean.getUserHeader());
                                param.put("telephone", edtPhone.getText().toString().trim());
                                param.put("code", edtCode.getText().toString().trim());
                                userRestUsage.loginByOther(loginByOtherId, param);
                            } else {
                                ToastUtil.showShort(BindPhoneActivity.this, "商家");
                                showProgressDialog("正在绑定...");
                                Map<String, String> param = new HashMap<String, String>();
                                param.put("userType", "1");
                                param.put("loginType", loginOtherBean.getLoginType());
                                param.put("account", loginOtherBean.getAccount());
                                param.put("username", loginOtherBean.getUsername());
                                param.put("userSex", loginOtherBean.getUserSex());
                                param.put("userArea", loginOtherBean.getUserArea());
                                param.put("userHeader", loginOtherBean.getUserHeader());
                                param.put("telephone", edtPhone.getText().toString().trim());
                                param.put("code", edtCode.getText().toString().trim());
                                userRestUsage.loginByOther(loginByOtherId, param);

                            }
//                    startActivity(new Intent(RegisterActivity.this, PerfectPersonInfoActivity.class));
                        }

                        @Override
                        public void onCancel() {

                        }
                    });
                    choiceJobDialog.show();
                }
                break;
        }
    }


    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
//            tvGetCode.setBackgroundColor(getColor(R.color.colorGray_D7));
            tvGetCode.setClickable(false);
//            tvGetCode.setText("(" + millisUntilFinished / 1000 + ") 秒后可重新发送");
            tvGetCode.setText(millisUntilFinished / 1000 + "s");
        }

        @Override
        public void onFinish() {
            tvGetCode.setText("重新获取验证码");
            tvGetCode.setClickable(true);
//            tvGetCode.setBackgroundColor(getColor(R.color.colorGray_777));
        }
    }
}
